<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules"  class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">用户注册</h3>
      </div>
       <el-form-item prop="name">
         <span class="svg-container">
           姓 名:
         </span>
         <el-input
           ref="name"
           v-model="loginForm.name"
           placeholder="请输入姓名"
           name="name"
           type="text"
           tabindex="1"
           size="medium "
           auto-complete="on"
         />
       </el-form-item>
      <el-form-item prop="username">
        <span class="svg-container">
          用 户 名:
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          size="medium "
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
       <span class="svg-container">
         密 码:
       </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
       <el-form-item prop="qpassword">
       <span class="svg-container">
         确认密码:
       </span>
        <el-input
          :key="passwordTypes"
          ref="qpassword"
          v-model="loginForm.qpassword"
          :type="passwordTypes"
          placeholder="请输入密码"
          name="qpassword"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwds">
          <svg-icon :icon-class="passwordTypes === 'qpassword' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="phone">
        <span class="svg-container">
          电 话:
        </span>
        <el-input
          ref="phone"
          v-model="loginForm.phone"
          placeholder="请输入电话"
          name="phone"
          type="text"
          tabindex="1"
          size="medium "
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="email">
        <span class="svg-container">
          邮 箱:
        </span>
        <el-input
          ref="email"
          v-model="loginForm.email"
          placeholder="请输入邮箱"
          name="email"
          type="text"
          tabindex="1"
          size="medium "
          auto-complete="on"
        />
      </el-form-item>
      <div class="submint">
        <el-button :loading="loading" type="primary" style="width:30%;margin-bottom:30px;" @click="Login">登陆</el-button>
        <el-button :loading="loading" type="primary" style="width:30%;margin-bottom:30px;" @click="register">注册</el-button>

      </div>

    </el-form>

  </div>

</template>

<script>
// import { validUsername } from '@/utils/validate'
import Method from '@/utils/proto'
import {posts} from '@/api/api'

export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入密码'))
      } else if (value.length <= 6) {
        callback(new Error('密码不能少于6位'))
      }else {
        callback()
      }
      
    }
    const validateEmail = (rule, value, callback) => {
      let regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      if (value == '' || !regEmail.test(value)) {
        callback(new Error('请输入正确邮箱'))
      } else {
        callback()
      }
    }
    const validatePhone = (rule, value, callback) => {
       let reg = /^1[0-9]{10}$/;
      if (value == "" ||value <= 10 || !reg.test(value)) {
        callback(new Error('请输入正确手机号'))
      } else {
        callback()
      }
    }
    const validateQpassword = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateName = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入姓名'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        method:Method.Method.MSG_AGENT_REGISTER,
        token:'',
        username: '',
        password: '',
        qpassword:'',
        name: '',
        phone: '',
        email: ''
      },
      loading: false,
      passwordType: 'password',
      passwordTypes: 'qpassword',
      redirect: undefined,
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        email: [{ required: true, trigger: 'blur', validator: validateEmail }],
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        qpassword: [{ required: true, trigger: 'blur', validator: validateQpassword }],
        name: [{ required: true, trigger: 'blur', validator: validateName }]
      },
    }
    
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showPwds() {
      if (this.passwordTypes === 'password') {
        this.passwordTypes = 'qpassword'
      } else {
        this.passwordTypes = 'password'
      }
      this.$nextTick(() => {
        this.$refs.qpassword.focus()
      })

    },
    create(){
       this.dialogFormVisiblea=true
    },
    register(){
      let data = this.loginForm
      if(data.name == ''||data.name == null){
        this.$message({
          type:'error',
          message:'姓名为空'
        })
        return
      }
      if(data.username == ''||data.username == null){
        this.$message({
          type:'error',
          message:'用户名为空'
        })
        return
      }
       if(data.password== undefined||data.password== null){
          this.$message({
              type:'error',
              message: `密码不能为空`
          })
          return
       }
       if(data.password.length <= 6){
          this.$message({
              type:'error',
              message: `密码不能少于六位`
          })
          return
       }
       if(data.password != data.qpassword){        
         this.$message({
          message: '密码不一致',
          type: 'error'
        });
         return
       }
       let reg = /^1[0-9]{10}$/;
       if (data.phone == "" ||data.phone <= 10 || !reg.test(data.phone)) {
        this.$message({
            message: '请输入正确的手机号',
            type: 'error'
        })
        return
      }
      let regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        if (data.email == '' || !regEmail.test(data.email)) {
            this.$message({
                message: '邮箱格式不正确',
                type: 'error'
            })
            return
       }
      if(data.qpassword == data.password){
         delete this.loginForm.qpassword
         posts(this.loginForm).then(res=>{
           if(res.result == 0){
            this.$message({
            type:'success',
            message:'注册成功'
           })
           }
        }).catch()       
        return
      }else{
        this.$message({
          type:'warning',
          message:'密码不一致'
        })
      }
    },
    Login(){
       this.$router.push({ path:'login'})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  
}
</style>

<style lang="scss" scoped>

$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.el-input{
  width: 60% !important;

}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    text-align: right;

    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 6rem;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .submint{
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>
